using IM.Easy.Infrastructure.Query;
using Microsoft.AspNetCore.Mvc;
using IM.Easy.SystemServices.Dtos;
using IM.Easy.SystemServices.IServices;
using IM.Easy.Entity.System;
using IM.Easy.Core.Attributes;

namespace IM.Easy.Api.Controllers.System
{
    /// <summary>
    /// OrgPostController 控制器
    /// </summary>
    [Route("system/orgpost")]
    public class OrgPostController : ControllerBase
    {
        private readonly ISysOrgPostService _service;

        /// <summary>
        /// 构造函数，注入服务
        /// </summary>
        /// <param name="service">服务接口</param>
        public OrgPostController(ISysOrgPostService service)
        {
            _service = service;
        }

        /// <summary>
        /// 获取所有SysOrgPost对象
        /// </summary>
        /// <returns>返回SysOrgPost列表</returns>
        [HttpGet]
        [Api(Perm = "orgpost:list", Name = "组织岗位列表")]
        public PageResponse<SysOrgPostDto> GetList([FromQuery] SysOrgPostQueryDto query)
        {
            return _service.GetPageList(query);
        }

        /// <summary>
        /// 根据ID获取SysOrgPost对象
        /// </summary>
        /// <param name="id">对象的ID</param>
        /// <returns>返回指定的SysOrgPost对象</returns>
        [HttpGet("{id}")]
        [Api(Perm = "orgpost:info", Name = "组织岗位详情")]
        public SysOrgPost GetById([FromRoute] long id)
        {
            var result = _service.GetById(id);
            return result ?? throw new KeyNotFoundException($"未找到ID为 {id} 的SysOrgPost对象。");
        }

        /// <summary>
        /// 创建新的SysOrgPost对象
        /// </summary>
        /// <param name="entity">要创建的SysOrgPost对象</param>
        /// <returns>返回创建后的SysOrgPost对象</returns>
        [HttpPost]
        [Api(Perm = "orgpost:add", Name = "新增组织岗位")]
        public SysOrgPost Create([FromBody] SysOrgPost entity)
        {
            _service.Create(entity);
            return entity; // 直接返回创建的实体
        }

        /// <summary>
        /// 更新现有的SysOrgPost对象
        /// </summary>
        /// <param name="id">对象的ID</param>
        /// <param name="entity">更新后的SysOrgPost对象</param>
        [HttpPut]
        [Api(Perm = "orgpost:edit", Name = "编辑组织岗位")]
        public void Update([FromBody] SysOrgPost entity)
        {
            _service.Update(entity);
        }

        /// <summary>
        /// 批量删除SysOrgPost对象
        /// </summary>
        /// <param name="ids">对象的IDS</param>
        [HttpDelete]
        [Api(Perm = "orgpost:delete", Name = "删除组织岗位")]
        public void Delete([FromBody] long[] ids)
        {
            _service.Delete(ids);
        }
    }
}
